//
//  HJWecomeController.swift
//  sinaWeibo
//
//  Created by hjun on 16/3/23.
//  Copyright © 2016年 hjun. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage
class HJWecomeController: UIViewController {
    
    ///头像底部约束
    
    var iconViewBottom: NSLayoutConstraint?
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        prepareUI()
        
        
        
        
        // 在发送网络请求之前,就先使用本地图片,如果有就是用本地图像,没有就使用默认的
        if let urlSting = HJUserAccountViewModel.sharedInstance.userAccount?.avatar_large{
            
            let url = NSURL(string: urlSting)!
            self.iconView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar_default_big"))
            
        }

        
        //获取用户信息 需要等网络请求了才加载
        HJUserAccountViewModel.sharedInstance.loadUserInfo { (error) -> () in
            
            if error != nil{
                
                print("请求用户信息出错")
                return
            }
            
        }
        
        
        ///没有出错的话 给头像赋值
        
        if let urlSting = HJUserAccountViewModel.sharedInstance.userAccount?.avatar_large{
            
            let url = NSURL(string: urlSting)!
            self.iconView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar_default_big"))
            
        }
        
        
    }
    
    private func prepareUI(){
        
        //添加子控件
        view.addSubview(bkgImageView)
        view.addSubview(iconView)
        view.addSubview(welocomeLabel)
        //添加约束
        bkgImageView.translatesAutoresizingMaskIntoConstraints = false
        iconView.translatesAutoresizingMaskIntoConstraints = false
        welocomeLabel.translatesAutoresizingMaskIntoConstraints = false
        //背景
        //        self.view.addConstraint(NSLayoutConstraint(item: bkgImageView, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        //
        //        self.view.addConstraint(NSLayoutConstraint(item: bkgImageView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        //
        //        self.view.addConstraint(NSLayoutConstraint(item: bkgImageView, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
        //
        //        self.view.addConstraint(NSLayoutConstraint(item: bkgImageView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        
        bkgImageView.snp_makeConstraints { (make) -> Void in
            
            make.edges.equalTo(self.view)
        }
        
        
        
        //头像
        //        self.view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        //        //底部约束
        //        iconViewBottom = (NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -160))
        //        self.view.addConstraint(iconViewBottom!)
        
        iconView.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(self.view)
            make.bottom.equalTo(self.view).offset(-160)
            make.size.equalTo(CGSize(width: 85, height: 85))
        }
        
        
        
        
        
        //欢迎label
        //        self.view.addConstraint(NSLayoutConstraint(item: welocomeLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        //
        //        self.view.addConstraint(NSLayoutConstraint(item: welocomeLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        //
        welocomeLabel.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(iconView)
            make.top.equalTo(iconView.snp_bottom).offset(16)
            
            
        }
        
        
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        moveAmination()
    }
    
    ///头像移动的动画
    private func moveAmination(){
        
        UIView.animateWithDuration(1, delay: 0.1, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            //self.iconViewBottom?.constant = -(UIScreen.mainScreen().bounds.height - 160)
            self.iconView.snp_updateConstraints(closure: { (make) -> Void in
                
                make.bottom.equalTo(self.view).offset(-(UIScreen.mainScreen().bounds.height - 160))
                
            })
            
            self.view.layoutIfNeeded()
            
            }) { (_) -> Void in
                
                UIView.animateWithDuration(HJDefaultDuration, animations: { () -> Void in
                    self.welocomeLabel.alpha = 1
                    }, completion: { (_) -> Void in
                        
                        AppDelegate.OutSwitchRootViewController(HJTabBarController())
                        
                })
                
        }
    }
    
    //MARK: 懒加载
    private lazy var bkgImageView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    private lazy var iconView: UIImageView = {
        
        let imageView = UIImageView(image: UIImage(named: "avatar_default_big"))
        
        //设置圆角
        imageView.layer.cornerRadius = 42.5
        imageView.layer.masksToBounds = true
        
        
        return imageView
    }()
    
    private lazy var welocomeLabel: UILabel = {
        
        let label = UILabel()
        label.text = "欢迎归来"
        label.font = UIFont.systemFontOfSize(14)
        label.sizeToFit()
        label.alpha = 0
        
        return label
        
    }()
    
    
    
}
